var pageSize = 10;
var pageNum = 1;
var page;
var laypage;
var list;
var layer;
var index;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入


        var start = {
            // min: laydate.now()
            max: '2099-06-16 23:59:59'
            , istoday: true
            , choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            // min: laydate.now()
            max: '2099-06-16 23:59:59'
            , istoday: false
            , choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function () {
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function () {
            end.elem = this
            laydate(end);
        }

    });
    setTimeout(function () {
        findStoreList();
    }, 200);

});


//查询商铺列表
function findStoreList() {
    var menuId = $("#menuId").val();
    var isPreferredBusiness = $("#isPreferredBusiness").val();
    var state = $("#state").val();
    var phone = $("#phone").val();
    var startTime = $("#LAY_demorange_s").val();
    var endTime = $("#LAY_demorange_e").val();
    if (phone != "") {
        if (!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) {
            alert("请输入有效的手机号码！");
            return false;
        }
    }

    var params = {
        "type": "query",
        "menuId": menuId,
        "pageSize": pageSize,
        "pageNum": pageNum,
        "state": state,
        "phone": phone,
        "isPreferredBusiness": isPreferredBusiness,
        "startTime": startTime,
        "endTime": endTime
    };
    $.ajax({
        type: "post",
        url: "/businessShop/findBusinessShopList",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data.code == 1 && data.code != undefined) {
                if(data.businessShops == null || data.businessShops.length <= 0){
                    $("#bus").html('<tr><td colspan="10" style="text-align: center">暂无数据</td></tr>');
                    $("#total").html("共有数据：0 条");
                    $("#page").css("display","none");
                    return;
                }
                list = data.businessShops;
                page = data.page;
                var html = "";
                for (var b in list) {
                    var state;
                    if (list[b].state == 0) {
                        state = "待审核";
                    } else if (list[b].state == 1) {
                        state = "审核通过";
                    } else if (list[b].state == 2) {
                        state = "审核未通过";
                    }
                    var isPreferredBusiness = "";
                    if (list[b].isPreferredBusiness == 0) {
                        isPreferredBusiness = "否";
                    } else if (list[b].isPreferredBusiness == 1) {
                        isPreferredBusiness = "是";
                    }

                    html += '<tr><td>' + list[b].userId + '</td>' +
                        '<td>' + (list[b].nickName == null ? "" : list[b].nickName) + '</td>' +
                        '<td>' + list[b].phone + '</td>' +
                        '<td>' + (list[b].createTime == null ? "" : new Date(list[b].createTime).toLocaleString()) + '</td>' +
                        '<td>' + (list[b].modifyTime == null ? "" : new Date(list[b].modifyTime).toLocaleString()) + '</td>' +
                        '<td>' + state + '</td>' +
                        '<td style="width: 320px;">' + (list[b].about == null ? "" : list[b].about) + '</td>' +
                        '<td>' + list[b].discount + '</td>' +
                        '<td>' + isPreferredBusiness + '</td>' +
                        '<td><button class="layui-btn layui-btn-normal" onclick="findDetails(' + b + ')">查看详情</button>';
                    if (sessionStorage.getItem(menuId + 'edit') == 1) {
                        if(list[b].state == 1){
                            if(list[b].isPreferredBusiness == 0){
                                html += '<button onclick="isPreferredBusiness(this,'+b+')" class="layui-btn layui-btn-primary">优选商家</button>';
                            }else if (list[b].isPreferredBusiness == 1){
                                html += '<button onclick="isPreferredBusiness(this,'+b+')" class="layui-btn layui-btn-normal">优选商家</button>';
                            }
                        }

                        if (list[b].state == 0) {
                            html += '<button class="layui-btn layui-btn-normal" onclick="showAuditPage(' + list[b].id + ')">审核</button>';
                        }
                    }
                    html += '</td></tr>';
                }
                $("#bus").html(html);
                $("#total").html("共有数据：" + page.total + " 条");
                laypage({
                    cont: 'page'
                    , pages: page.pages
                    , first: 1
                    , curr: pageNum
                    , last: page.pages
                    , prev: '<em><</em>'
                    , next: '<em>></em>'
                    , jump: function (obj, first) {
                        pageNum = obj.curr;
                        if (!first) {
                            findStoreList();
                        }
                    }
                });
            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
            }
        }

    });
}


//查询按钮
function search() {
    pageNum = 1;
    findStoreList();
}

//查看详情
function findDetails(b) {


    var bs = list[b];

    var videoInfo = "";
    if(bs.firstVideo != null){
        videoInfo = bs.firstVideo.split(";")[0];
    }
    var firstPic = "";
    if(bs.firstPic != null){
        firstPic = bs.firstPic.split(";")[0];
    }
    var secondPic = "";
    if(bs.secondPic != null){
        secondPic = bs.secondPic.split(";")[0];
    }
    var thirdPic = "";
    if(bs.thirdPic != null){
        thirdPic = bs.thirdPic.split(";")[0];
    }
    var fourPic = "";
    if(bs.fourPic != null){
        fourPic = bs.fourPic.split(";")[0];
    }
    var fivePic = "";
    if(bs.fivePic != null){
        fivePic = bs.fivePic.split(";")[0];
    }
    var sixPic = "";
    if(bs.sixPic != null){
        sixPic = bs.sixPic.split(";")[0];
    }

    var html = '<table border="1px" style="width: 98%;border-spacing: 10px;margin: 10px 10px 100px 10px;border-color:darkgrey "><tr><td>店铺属性</td><td>配图</td><td>链接地址</td></tr>'+
        '<tr><td>店铺招牌图片</td><td><img src="'+bs.shopSign+'" style="widows: 200px;height: 100px;"></td><td>'+bs.shopSign+'</td></tr>'+
        '<tr><td>店铺视频</td><td><video src="'+videoInfo+' " controls="controls" style="width: 200px;height: 100px"></video></td><td>'+videoInfo+'</td></tr>'+
        '<tr><td>产品介绍</td><td><img src="'+firstPic+'"style="widows: 200px;height: 100px;"></td><td>'+firstPic+'</td></tr>'+
        '<tr><td>产品介绍</td><td><img src="'+secondPic+'"style="widows: 200px;height: 100px;"></td><td>'+secondPic+'</td></tr>'+
        '<tr><td>产品介绍</td><td><img src="'+thirdPic+'"style="widows: 200px;height: 100px;"></td><td>'+thirdPic+'</td></tr>'+
        '<tr><td>产品介绍</td><td><img src="'+fourPic+'"style="widows: 200px;height: 100px;"></td><td>'+fourPic+'</td></tr>'+
        '<tr><td>产品介绍</td><td><img src="'+fivePic+'"style="widows: 200px;height: 100px;"></td><td>'+fivePic+'</td></tr>'+
        '<tr><td>产品介绍</td><td><img src="'+sixPic+'"style="widows: 200px;height: 100px;"></td><td>'+sixPic+'</td></tr></table>';

    layer.open({
        title: ['店铺内容清单', 'color: #2299ee;border-bottom: 1px solid;']
        , type: 1
        , content: html
        , area: ['90%', '80%']
        , maxmin: true
    });
    $(".layer-anim").css("animation-name","inherit");

}

//打开审核页面
function showAuditPage(id) {
    var html = "<textarea id='content' style='width: 497px;height: 180px;resize: none'></textarea><button id='bt1' onclick='through(" + id + "," + 1 + ",this)' class='layui-btn layui-btn-normal' style='margin-left: 100px !important;'>通过</button><button id='bt2' onclick='through(" + id + "," + 2 + ",this)' class='layui-btn layui-btn-danger' style='margin-left: 100px !important;'>不通过</button>"
    index = layer.open({
        title: ['店铺审核', 'color: #2299ee;border-bottom: 1px solid;']
        , type: 1
        , content: html
        , area: ['500px', '300px']
        , maxmin: true
    });
}

//审核通过
function through(id, auditType, obj) {
    $("#bt1").attr("disabled", true);
    $("#bt1").attr("class", "layui-btn layui-btn-disabled");
    $("#bt2").attr("disabled", true);
    $("#bt2").attr("class", "layui-btn layui-btn-disabled");
    $(obj).attr("disabled", true);
    var content = $("#content").val();
    if (auditType == 2) {
        if (content == "" || content == undefined) {
            alert("请填写审核信息");
            $(obj).attr("disabled", false);
            $(obj).attr("class", "layui-btn layui-btn-danger");
            $("#bt1").attr("disabled", false);
            $("#bt1").attr("class", "layui-btn layui-btn-normal");
            return false;
        }
    }else {
        content = "亲，恭喜您，您的主页已经审核通过啦，快去进行首页推广吧。";
    }
    var menuId = $("#menuId").val();
    var params = {
        "type": "change",
        "menuId": menuId,
        "id": id,
        "auditType": auditType,
        "content": content
    };
    $.ajax({
        type: "post",
        url: "/businessShop/auditBus",
        data: params,
        dataType: "json",
        success: function (data) {

            if (data.code == 1 && data.code != undefined) {
                alert("审核成功");
                layer.close(index);
                findStoreList();
            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
                layer.close(index);
                findStoreList();
            } else if (data.code == 2 && data.code != undefined) {
                alert("审核失败");
                layer.close(index);
                findStoreList();
            } else if (data.code == 3 && data.code != undefined) {
                alert("审核失败");
                layer.close(index);
                findStoreList();
            }
        }
    });
}
//设置是否为优选商家
function isPreferredBusiness(obj,b) {

    var menuId = $("#menuId").val();
    var isPreferredBusiness = list[b].isPreferredBusiness;
    var id = list[b].id;
    if(isPreferredBusiness == 0){
        isPreferredBusiness = 1;
    }else {
        isPreferredBusiness = 0;
    }
    $(obj).attr("class","layui-btn layui-btn-disabled");
    var params = {
        "type": "change",
        "menuId": menuId,
        "id":id,
        "isPreferredBusiness":isPreferredBusiness
    };
    $.ajax({
        url: "/businessShop/updateIsPreferredBusiness",
        type: "post",
        data: params,
        dataType: "json",
        success:function (data) {
            if(data.code == 1 && data.code != undefined){
                alert(data.message);
                findStoreList();
            } else if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
                findStoreList();
            } else if (data.code == 2 && data.code != undefined) {
                alert(data.message);
                findStoreList();
            } else if (data.code == 3 && data.code != undefined) {
                alert("修改失败");
                findStoreList();
            }
        }
    });
    
}